package cn.suse.android.net;

import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import android.R.integer;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;

import com.kaixin.connect.Kaixin;

public class AppHelper {

	private static final int RETRY_TIME = 3;
	
	public static String getFriendActive(Kaixin kaixin,Context context,String url,Bundle bundle,String method){
		if(kaixin == null||context == null||url == null||url.trim().equals("")||method==null||method.trim().equals("")){
			return null;
		}
		int times = 0;
		String json = null;
		do {
			try {
				String jsonString = kaixin
						.request(context, url, bundle, method);
				if (jsonString != null && !jsonString.trim().equals("")) {
					json = jsonString;
					break;
				}
			} catch (Exception e) {
				times++;
				e.printStackTrace();
			}
		} while (times<RETRY_TIME);
		System.out.println(json);
		return json;
	}
	public static Bitmap getNetBitmap(String s_url){
		Bitmap bitmap = null;
		int times =0;
		do {
			try {
				URL url = new URL(s_url);
				HttpURLConnection connection = (HttpURLConnection) url
						.openConnection();
				connection.connect();
				if (connection.getResponseCode() == 200) {
					bitmap = BitmapFactory.decodeStream(connection.getInputStream());
					if(bitmap!=null){
						break;
					}else{
						times++;
						continue;
					}
				}
			} catch (Exception e) {
				times++;
			}
		} while (times<RETRY_TIME);
		return bitmap;
	}
	public static String getCircleIds(Kaixin kaixin,Context context,String url) {
		return getFriendActive(kaixin, context, url, null, "GET");
	}
}
